/*
 * iris.c
 *
 *  Created on: Oct 1, 2013
 *      Author: cinus
 */

#include <stdio.h>
#include <stdlib.h>

#include "jjann.h"
#include "net.h"
#include "training.h"

int main(int argc, char** argv) {
	uint8 status;
	int32 j;
	float32 error;
	float32 output[3];

	trainingData_t trainingData;
	trainingData_t validationData;
	netdata_tp net;

	//topology_t topo;

	srand(123456);
	fprintf(stderr, "Program started\n");
	/*status = read(&topo, "samples/iris-classification/topology/463.top");
	if (status != EXIT_SUCCESS) {
		fprintf(stderr, "Error, aborting software\n");
		exit(status);
	} else {
		fprintf(stderr, "Topology successfully acquired\n");
	}
	*/
	//net = initNetwork(&topo, "configs/iris.cfg");
	net = initNetwork("configs/iris.cfg");
	if (net == NULL ) {
		fprintf(stderr, "Network creation failed, aborting\n");
		exit(EXIT_FAILURE);
	} else {
		fprintf(stderr, "Network successfully created\n");
	}
	status = loadSamples(&trainingData, "datasets/iris.training");
	if (status != EXIT_SUCCESS) {
		fprintf(stderr, "Error, aborting software\n");
		exit(status);
	} else {
		fprintf(stderr, "Training data loaded\n");
	}

	status = loadSamples(&validationData, "datasets/iris.validation");
	if (status != EXIT_SUCCESS) {
		fprintf(stderr, "Error, aborting software\n");
		exit(status);
	} else {
		fprintf(stderr, "Validation data loaded\n");
	}
	error = .0;
	for (j = 0; j < net->settings.maxEpochs; j++) {
		error = trainOneEpoch(net, trainingData.samples, trainingData.sampleSize);
	}
	fprintf(stderr,"Trained for %d epochs\n", net->settings.maxEpochs);
	feedForward(net, validationData.samples[0].input, output);
	error = validate(net, validationData.samples, validationData.sampleSize);
	fprintf(stderr, "Validation error: %f\n", error);
	destroyNetwork(net);
	freeSamples(&trainingData);
	freeSamples(&validationData);
	return 0;
}
